/*
 * Module 02
 *
 */

package ua.gl.basecamp.tabachnyy.testcase;

import ua.gl.basecamp.tabachnyy.util.UserAnswersParser;

import java.util.List;

/**
 * @author yaroslav.tabachnyy@gmail.com
 * @version 1.02
 * @date 14.02.2013
 *
 */
public class SingleAnswerQuestion extends Question {
    private static final double MAX_SCORE = 1.0;
    private static final double MIN_SCORE = 0.0;

    public SingleAnswerQuestion(String stringRepresentation, List<Answer> answers) {
        super(stringRepresentation, answers);
    }


    @Override
    public double getAnswersScore(List<UserAnswer> userAnswers) {
        final int ANSWER_INDEX = 0;

        if (userAnswers.size() != 1) {
            throw new IllegalArgumentException("Must be exactly 1 answer!");
        }

        for (Answer answer : getAnswers()) {
            if (answer.isCorrect()
                    && answer.equals(userAnswers.get(ANSWER_INDEX)))
                return MAX_SCORE;
        }
        return MIN_SCORE;
    }

    @Override
    public List<UserAnswer> getAnswersFromString(String userAnswersString) {
        UserAnswersParser parser = new UserAnswersParser(userAnswersString);
        return parser.getAnswersByNumbers(this);
    }

    public String toString() {
        String questionString = getStringRepresentation() + "(single answer)\n";
        questionString += getAnswersString();

        return questionString;
    }
}
